package com.hbk.programe;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Locale;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hbk.bo.Application;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.utility.StringUtil;

public class CodeMakerApplication {
	
	private static Configuration freemarkerCfg = new Configuration();
	
	public static void main(String[] args) {
		buildModular("dms/order.json");
	}

	private static void buildModular(String str) {
		try {
			File file=new File("src/main/resources/modular/json/"+str);
			String json=FileUtils.readFileToString(file, "UTF-8");
			Modular modular=JSON.parseObject(json, new TypeReference<Modular>() {});
			
			File[] files=file.getParentFile().listFiles();
			for(File f:files) {
				json=FileUtils.readFileToString(f, "UTF-8");
				Modular pojo=JSON.parseObject(json, new TypeReference<Modular>() {});
				modular.getPojos().add(pojo);
			}
			if(modular.getFields().size()>0) {
				buildFile("modular/ftl/entity.ftl","src\\java\\com\\hbk\\pojos\\"+modular.getClazz()+".java",modular,"UTF-8");
			}
			for(Dto dto:modular.getDtos()) {
				if(dto.getClazz().endsWith("Query")) {
					buildFile("modular/ftl/query.ftl","src\\java\\com\\hbk\\dto\\"+dto.getClazz()+".java",dto,"UTF-8");
				}else {
					buildFile("modular/ftl/dto.ftl","src\\java\\com\\hbk\\dto\\"+dto.getClazz()+".java",dto,"UTF-8");
				}
			}
			if(modular.getMethods().size()>0) {
				for(Method method:modular.getMethods()) {
					method.setModular(modular);
				}
				buildFile("modular/ftl/service.ftl","src\\java\\com\\hbk\\service\\I"+modular.getClazz()+"Service.java",modular,"UTF-8");
				buildFile("modular/ftl/serviceImpl.ftl","src\\java\\com\\hbk\\service\\impl\\"+modular.getClazz()+"ServiceImpl.java",modular,"UTF-8");
				buildFile("modular/ftl/controller.ftl","src\\java\\com\\hbk\\controller\\"+modular.getClazz()+"Controller.java",modular,"UTF-8");
			}
			Page page=modular.parsePages();
			if(page!=null&&page.getPattern().equals("first")) {
				buildFile("modular/ftl/first/index.ftl","src\\pages\\"+page.getNamespace()+"\\index.js",page,"UTF-8");
				buildFile("modular/ftl/first/modal.ftl","src\\pages\\"+page.getNamespace()+"\\modal.js",page,"UTF-8");
				List<Form> fields=page.getForms();
				boolean models=false;
				for (Form form : fields) {
					if(form.getType().equals("DataList")) {
						models=true;
						buildFile("modular/ftl/first/modelDataList.ftl","src\\pages\\"+page.getNamespace()+"\\models\\model"+StringUtil.capitalize(form.getName())+".js",form,"UTF-8");
					}
				}
				buildFile("modular/ftl/first/model.ftl","src\\pages\\"+page.getNamespace()+(models?"\\models":"")+"\\model.js",page,"UTF-8");
			}else if(page!=null&&page.getPattern().equals("second")) {
				buildFile("modular/ftl/first/index.ftl","src\\pages\\"+page.getNamespace()+"\\index.js",page,"UTF-8");
				buildFile("modular/ftl/first/modal.ftl","src\\pages\\"+page.getNamespace()+"\\modal.js",page,"UTF-8");
				List<Form> fields=page.getForms();
				for (Form form : fields) {
					if(form.getType().equals("DataList")) {
						buildFile("modular/ftl/first/modelDataList.ftl","src\\pages\\"+page.getNamespace()+"\\models\\model"+StringUtil.capitalize(form.getName())+".js",form,"UTF-8");
					}else if(form.getType().equals("Table")) {
						for(Column col:form.getColumns()) {
							buildFile("modular/ftl/first/modelDataList.ftl","src\\pages\\"+page.getNamespace()+"\\models\\model"+StringUtil.capitalize(form.getName())+StringUtil.capitalize(col.getName())+".js",col.getForm(),"UTF-8");
						}
					}
				}
				buildFile("modular/ftl/first/model.ftl","src\\pages\\"+page.getNamespace()+"\\models\\model.js",page,"UTF-8");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void buildFile(String templateName, String fileName, Object obj,String encoding) throws Exception {
		try {
			freemarkerCfg.setClassForTemplateLoading(Application.class, "/");
			freemarkerCfg.setEncoding(Locale.getDefault(), encoding);
			Template template = freemarkerCfg.getTemplate(templateName);
			template.setEncoding("UTF-8");
			File htmlFile = new File(fileName);
			htmlFile.getParentFile().mkdirs();
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), encoding));
			template.process(obj, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
